<?php
namespace app\admin666\controller;

use think\Db;
use think\Validate;

class Column extends Admin
{
    /**
    * 模型管理
    * @date: 2018年6月7日 上午10:26:48
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function index(){
	    $map = [];
		$order="id desc";
		
		$list = Db::name('column')->where($map)->select();
		
		import('util.Tree');
		//$tree = \Tree::makeTree($list,['expanded' => true]);
		$tree = \Tree::makeTreeForHtml($list,['expanded' => true]);
		
		foreach ($tree as $key=>$val){
		    $flag = '';
		    if(isset($val['level']) && $val['level'] > 0){
		        for ($i=0;$i<$val['level'];$i++){
		            $flag .= $i==0?'┠─':'──';
		        }
		    }
		    $tree[$key]['flag'] = $flag;
		}
		
		$this->assign('_list', $tree);
		
		return $this->fetch();
    }
    
    /**
    * 添加
    * @date: 2018年6月7日 上午10:32:54
    * @author: xiaocui
    * @param: $GLOBALS
    * @return:
    */
    public function add($pid=0){
        if(request()->isPost()){
            $rule = [
                'title' =>  'require',
            ];
            
            $msg = [
                'title.require' => '请填写栏目名称名称',
            ];
            $validate = new Validate($rule,$msg);
            
            if(!$validate->check(input('post.'))){
                $this->error($validate->getError());
            }
            
            $model = model('column');
            $data = input('post.');
            if($model->save($data)){
                $this->success('添加成功！',url('index'));
            }else{
                $this->error($model->getError());
            }
    
        }else{
            $mx = model('models')->field('id,title,temp_index,temp_list,temp_view')->where(['status'=>1])->select();
            $this->assign('_mx',$mx);
            $this->assign('_tree',model('column')->get_tree('','id,pid,title'));
            if($pid > 0){
                $this->assign('pinfo',model('column')->find($pid));
            }
            return $this->fetch('edit');
        }
    }
    
    /**
    * 编辑
    * @date: 2018年6月7日 上午10:50:46
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function edit($id=0){
        if(request()->isPost()){
            $rule = [
                'title' =>  'require',
            ];
            
            $msg = [
                'title.require' => '请填写栏目名称名称',
            ];
            $validate = new Validate($rule,$msg);
            
            if(!$validate->check(input('post.'))){
                $this->error($validate->getError());
            }
            
            $model = model('column');
            $data = input('post.');
            if($model->save($data,['id'=>$id])){
                $this->success('编辑成功！',url('index'));
            }else{
                if($model->getError()){
                    $this->error($model->getError());
                }else{
                    $this->success('编辑成功！','');
                }
            }
        }else{
            $info = model('column')->find($id);
            $this->assign('info',$info);
            
            $mx = model('models')->field('id,title,temp_index,temp_list,temp_view')->where(['status'=>1])->select();
            $this->assign('_mx',$mx);
            $this->assign('_tree',model('column')->get_tree('','id,pid,title'));
            return $this->fetch();
        }
    }
    
    /**
    * 删除
    * @date: 2018年6月7日 上午10:50:56
    * @author: xiaocui
    * @param: $GLOBALS
    * @return:
    */
    public function del($id=0){
        $model = model('column');
        if(($model->destroy($id))){
            //记录行为
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
    
    /**
     * 排序
     * @date: 2018年6月2日 上午10:48:58
     * @author: xiaocui
     * @param: variable
     * @return:
     */
    public function sort(){
        if(request()->isPost()){
            $post = input('post.');
            if(isset($post['ids']) && isset($post['sort'])){
                $data = [];
                foreach ($post['ids'] as $key=>$val){
                    $data[$key]['id'] = $val;
                    $data[$key]['sort'] = intval($post['sort'][$key]);
                }
    
                if(model('column')->saveAll($data,true)){
                    $this->success('排序成功！');
                }
    
            }
            $this->error('提交失败！');
        }
    }
    
    /**
     * 状态
     * @date: 2018年6月2日 上午10:48:58
     * @author: xiaocui
     * @param: variable
     * @return:
     */
    public function status($id,$display){
        $id = intval($id);
        $display = intval($display);
        $model = model('column');
        if($model->save(['display'=>$display],['id'=>$id])){
            $this->success('修改成功','');
        }else{
            $this->error('修改失败！');
        }
    }
}
